package com.javaee.term.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaee.term.exception.HandException;
import com.javaee.term.model.LogistInfo;
import com.javaee.term.model.Product;
import com.javaee.term.model.SaleMan;
import com.javaee.term.service.ProductService;
import com.javaee.term.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * author 陈伯瑞
 * date 2019/12/22 0022 14:30
 * version 1.0
 **/
@Controller
@RequestMapping("/seller/product")
@Slf4j
public class ProductController {
    @Autowired
    ProductService productService;
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "5") Integer size,
                             @RequestParam(value = "code", defaultValue = "0") Integer code,
                             Map<String, Object> map) {

        PageHelper.startPage(page,size);
        List<Product> list=productService.findAll();
        PageInfo<Product> productPageInfo=new PageInfo<Product>(list);
        map.put("productPageInfo", productPageInfo);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("code", code);
        map.put("totalpage", productPageInfo.getPages());
        return new ModelAndView("product/list", map);
    }
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId", required = false) String  productId,
                              Map<String, Object> map) {
        if (!StringUtils.isEmpty(productId)) {
            Product product = productService.findOne(productId);
            map.put("productInfo", product);
        }

        return new ModelAndView("product/index", map);
    }

    @PostMapping("/save")
    @Transactional
    public ModelAndView save(@Valid Product form,
                             BindingResult bindingResult,
                             Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/product/index");
            return new ModelAndView("common/error", map);
        }

        Product product = new Product();
        try {
            //如果Id为空, 说明是新增
            if (!StringUtils.isEmpty(form.getProductId())) {
                product = productService.findOne(form.getProductId());
                BeanUtils.copyProperties(form, product);
                productService.update(product);
            } else {
                form.setProductId(KeyUtil.genUniqueKey());
                BeanUtils.copyProperties(form, product);
                try {
                    productService.insert(product);
                }
                catch (HandException e) {
                    map.put("msg", e.getMessage());
                    map.put("url", "/sell/seller/product/list?code=1");
                    return new ModelAndView("common/error", map);
                }
            }

        } catch (HandException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/product/list?code=1");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/sell/seller/product/list?code=1");
        return new ModelAndView("common/success", map);
    }

}

